#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Last Update: 2010/10/30 11:14:40

'''mouse control
'''

__revision__ = '0.1'
__author__ = 'lxd'

import win32gui,win32api,win32con
import tools
import random

class Mouse():
    def __init__(self,log,window):
        self.log = log
        self.w = window
        self.hwnd = self.w.hwnd


    def click(self,x,y):
        #win32api.SetCursorPos((x,y))
        #tools.sleep()
        tmp = win32api.MAKELONG(x,y)
        win32api.SendMessage(self.hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, tmp) 
        win32api.SendMessage(self.hwnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, tmp)
        self.log.debug('click on (%s,%s)' %(x,y))
        tools.sleep()


    def moveTo(self,pos):
        pass


    def clickIfColor(self,x,y,color):
        if self.w.getColor(x,y) == color:
            self.click(x,y)
            return True
        else:
            return False


    def clickInRegion(self,x0,y0,x1,y1):
        #random click in region(x0,y0,x1,y1)
        x = random.randint(x0,x1)
        y = random.randint(y0,y1)
        self.click(x,y)

#########################################
    def getCurPos(self):
        #get current cursor's position
        return win32gui.GetCursorPos()

    def getWinHandleFromPoint(self,pos):
        #get window from point
        return win32gui.WindowFromPoint(pos)


if __name__ == '__main__':
    import config
    import window
    w = window.Window()
    m = Mouse(config.log,w)
    m.click(10,10)


